<?php
namespace Admin\Controller;
use Think\Controller;
class NoticeController extends BaseController {
    private $createnotice_fields = array('title', 'info ','add_time',);
    private $editnotice_fields = array('title',  'info', 'update_time');
    
    public function index(){
        $notice = D('notice');
        $count = $notice->count(); 
        $p = getpage($count,C('PAGE_SIZE'));
        $show = $p->show(); 
        $list = $notice->page(I('get.p'))->order('id desc')->limit(C('PAGE_SIZE'))->select();
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $list);
        $this->display();
    }
    
    public function add(){
        if (IS_POST) {
            $data = $this->addCheck();
            $obj = D('notice');
            if ($obj->add($data)) {
                $this->success("保存成功", U('Notice/index'));
                return;
            }else {
            $this->error('操作失败！');
            return;
         } 
        }
        $this->assign('list', $list);
        $this->display();
    }

    private function addCheck() {
        $param = I('post.');
        $data = $this->checkFields($param["data"], $this->createnotice_fields);
        $add_time = strtotime(date("Y-m-d H:i:s",time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if (D('notice')->getNoticeTitle($data['title'])) {
            $this->error('消息已经存在');
            exit;
        }
        if (empty($data['title'])) {
            $this->error('消息标题不能为空');
        }
        $data['info'] = $_POST['info'];
        $data['add_time'] = $add_time;
        return $data;
    }
    
    public function edit($id = 0) {
        if ($id = (int) $id) {
            $obj = D('notice');
            if (!$detail = $obj->find($id)) {
                $this->error('请选择要编辑的系统消息');
            }
            if (IS_POST) {
                $data = $this->editCheck();
                $data['id'] = $id;  
                if ($obj->save($data)) {
                    $this->success('操作成功', U('Notice/index'));
                } else {
                    $this->error('操作失败');
                }
            } else {
                $this->assign('list', $list);
                $this->assign('detail', $detail);
                $this->display();
            }
        } else {
            $this->error('请选择要编辑的系统消息');
        }
    }

    private function editCheck() {
        $param = I('post.');
        $data = $this->checkFields($param['data'], $this->editnotice_fields);
        $update_time = strtotime(date("Y-m-d H:i:s",time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if (empty($data['title'])) {
            $this->error('消息标题不能为空');
        }
        $data['info'] = $_POST['info'];
        $data['update_time'] = $update_time;
        return $data;
    }
    
    public function delnotice() {
        $notice = D('notice');
        $id = I('get.id');
        $result = $notice->where('id =' . $id . '')->delete();
        if ($result) {
            $this->success('删除成功', U('Notice/index'));
        } else {
            $this->error('删除失败');
        }
    }
}

      